using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MediaPlayerToolBar
{
	public enum MoveAction{ xPlus,xMiuns,yPlus,yMiuns, nothing};
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label label1;	
		
		
		MP mp = new MP();
		bool isCtrl = false;
		bool isDrag = false;
		int moveCount = 0;
		static int MAXMOVE = 25;
		Point[] movePoint = new Point[MAXMOVE] ;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(0, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(2, 6);
			this.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.ClientSize = new System.Drawing.Size(122, 15);
			this.ControlBox = false;
			this.Controls.Add(this.label1);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 3.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "Form1";
			this.Opacity = 0.5;
			this.ShowInTaskbar = false;
			this.Text = "Enjoy With Binish :-)";
			this.TopMost = true;
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
			this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
			this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
			this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
			this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
			this.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseWheel);
			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
			this.Click +=new EventHandler(Form1_Click);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_MouseWheel(object sender, MouseEventArgs e)
		{	
			
			int delta =  e.Delta ;				
			if(isCtrl)
			{				
				if(e.Delta  > 0 && this.Opacity < 1.0)
				{
					this.Opacity += 0.05;
				}
				else if(e.Delta  < 0 && this.Opacity > 0.05)
				{
					this.Opacity -= 0.05;
				}
			}
			else
			{
				if(e.Delta  > 0)
					mp.VolumeIncrease();
				else
					mp.VolumeDecrease();
			}
		}
		private void Form1_DoubleClick(object sender, EventArgs e)
		{
			
			mp.PlayPause();
		}

		private void Form1_MouseDown(object sender, MouseEventArgs e)
		{
			
			if( e.Button == MouseButtons.Right)
			{
				mp.Next();
			}
			else if( e.Button == MouseButtons.Middle)
			{
				mp.Last();
			}
			else if( e.Button == MouseButtons.Left & !isCtrl)
			{
				moveCount =0;
				isDrag = true;				
			}
			else if( e.Button == MouseButtons.Left & isCtrl)
			{
				this.Close();
			}
		}
		MoveAction isLine(Point[] points,bool isX)
		{
			int factor  = 5;
			int last = getP(points[0],isX);

			int max = last + factor;
			int min = last - factor;
			
			foreach(Point point in points)
			{	
				int current = getP(point,isX); 
				if(current < max && current > min )
					continue;
				else
					return MoveAction.nothing;
			}
			if ( getP(points[0],!isX) > getP(points[points.Length - 1],!isX))
			{
				return (isX)? MoveAction.xPlus : MoveAction.yPlus;
			}			
			else
			{
				return (isX)? MoveAction.xMiuns :MoveAction.xMiuns;
			}
		}
		MoveAction whichLine(Point[] points)
		{
			MoveAction ma = isLine(points,true);
			if(ma != MoveAction.nothing)
				return ma;			
			else 
				return isLine(points,false);
		}
		int getP(Point p,bool isX)
		{
			return (isX)?p.X:p.Y;
		}
		private void Form1_MouseMove(object sender, MouseEventArgs e)
		{				
			if(isDrag)
			{
				movePoint[moveCount] = new Point(e.X,e.Y);
				moveCount ++;
				//label1.Text = "move "+ moveCount.ToString();
				if(moveCount > MAXMOVE - 1)
				{
					//label1.Text = "reached ";
				
					MoveAction ma = whichLine(movePoint);					

					moveCount = 0;
				}				
					
			}
			else
			{
				moveCount = 0;				
			}

		}

		private void Form1_KeyDown(object sender, KeyEventArgs e)
		{
			isCtrl = e.Control;			
		}

		private void Form1_KeyUp(object sender, KeyEventArgs e)
		{
			isCtrl = e.Control;			
		}

		private void Form1_MouseUp(object sender, MouseEventArgs e)
		{
			isDrag = false;						
		}

		private void Form1_Click(object sender, EventArgs e)
		{
			
				
		}
	}
}
